package com.easyattendance.services;


import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.easyattendance.model.Class;
import com.easyattendance.model.Parent;
import com.easyattendance.model.Student;
import com.easyattendance.model.Teacher;
import com.easyattendance.util.HibernateUtil;

public class AddParentService {
	
	
	/* Method to CREATE a parent record in the database */
	   public synchronized boolean addParent(Parent parent){
	      Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      boolean result=false;
	      
	      try{
	         tx = session.beginTransaction();

	        	 session.save(parent);
	           	 tx.commit();
	           	result=true; 

	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return result;
	   }
	   
	/* Method to CREATE a Parent and Student record in the database */
	   public synchronized boolean addParentAndStudent(Parent parent,Student student){
	      Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      boolean result=false;
	      
	      try{
	         tx = session.beginTransaction();
	       
	         
	        student.setParent(parent);
	        Set<Student> students=new HashSet<Student>();
	        students.add(student);
	        parent.setStudents(students);
	       
	         session.save(parent);
	         session.save(student);
	       
	         tx.commit();
	         
	         result=true;
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return result;
	   }
	   
	   /* Method to UPDATE a Parent in the database */
	   public synchronized boolean updateParentAndStudent(Parent parent,Student student){
	      Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      boolean result=false;
	      
	      try{
	         tx = session.beginTransaction();
	        student.setParent(parent);
	        Set<Student> students=new HashSet<Student>();
	        students.add(student);
	        parent.setStudents(students);
	       
	         session.update(parent);
	         //session.update(student);
	       
	         tx.commit();
	         
	         result=true;
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return result;
	   }
	   
	   public int getParentIdValue()
	   {
		   	Session session = HibernateUtil.openSession();
	        Transaction tx = null;
	        int value=0;
	        try {
	            tx = session.getTransaction();
	            tx.begin();
	            SQLQuery query = session.createSQLQuery("select MAX(parent_id) from tbl_parent");
	           
	            if(query.uniqueResult()!=null)
	            {
	            	String parent_id=(String) query.uniqueResult();
	            	String parent_counter=parent_id.substring(6);
	            	value = Integer.parseInt(parent_counter)+1;
	            }
	            else
	            	value=1;
	            
	            tx.commit();
	        } catch (Exception e) {
	            if (tx != null) {
	                tx.rollback();
	            }
	            e.printStackTrace();
	        } finally {
	            session.close();
	        }
		   
		   return value;
	   }
	   
	public boolean ifAlreadyExists(Parent newParent) {
		boolean flag = false;

		Session session = HibernateUtil.openSession();
		Transaction tx = null;
		List parents = null;
		try {
			tx = session.beginTransaction();
			parents = session.createQuery("FROM Parent").list();
			for (Iterator iterator = parents.iterator(); iterator.hasNext();) {
				Parent parent = (Parent) iterator.next();
				if (parent.equals(newParent)) {
					System.out.println("Parent already exists");
					flag = true;
					break;
				}

			}
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return flag;
	}
	
	public Parent getParentByEmailId(String parentEmailId)
	{
		 Session session = HibernateUtil.openSession();
		 Transaction tx = null;
		
		 Query query=null;
		 Parent parent = null;
		
		 try {
			 tx = session.getTransaction();
			 tx.begin();
			
			 query = session.createQuery("from Parent where parent_email_id='"+parentEmailId+"'");
			 parent = (Parent) query.uniqueResult();
			 
			 tx.commit();
	        } catch (Exception e) {
	            if (tx != null) {
	                tx.rollback();
	            }
	            e.printStackTrace();
	        } finally {
	            session.close();
	        }
	
		return parent;
	}

}
